package com.easy.work.http;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.easy.work.app.CommonValue;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.protocol.HTTP;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by zzgpc on 2017-9-26.
 */

public class UploadImgClient extends AsyncTask<String, Integer, String> {

    private UploadCallback callback;

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection conn = null;
        try {
            // URL url = new URL( CommonValue.BASE_NALI_URL + "/logon/login" );
            URL url = new URL(CommonValue.HostServerUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("charset", HTTP.UTF_8);
//            conn.setRequestProperty("X-Access-token", userLoginInfo.getToken_nali_login());
            conn.setRequestProperty("X-Service-Method", "pictureUpload");
            conn.setRequestProperty("X-Service-Id", "tutor.fileUploadService");
//            conn.setRequestProperty("X-Client-Id", userLoginInfo.getClientID());
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true); // 允许输入流
            conn.setDoOutput(true); // 允许输出流
            conn.setUseCaches(false); // 不允许使用缓存
            conn.setRequestMethod("POST"); // 请求方式
            conn.setRequestProperty("Charset", "utf-8"); // 设置编码
            conn.setRequestProperty("connection", "keep-alive");
//            conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="
//                    + BOUNDARY);
            RequestParams requestParams = new RequestParams();
            // post请求的参数
            // String data = "username=" + username + "&password=" + password;
            OutputStream out = conn.getOutputStream();
            out.write(params[2].getBytes());
            out.flush();
            out.close();

            conn.connect();
            int code = conn.getResponseCode();
            if (code == 200) {
                InputStream is = conn.getInputStream();
//                String state = FileUtil.getStringFromInputStream(is);
                return "";
            } else {
//                String error = FileUtil.getStringFromInputStream(conn.getErrorStream());
            }
            if (conn != null) {
                conn.disconnect();
            }

        } catch (final Exception e) {
            e.printStackTrace();
            callback.onFailure(e.getMessage());
        }

        return null;
    }


    /**
     * 运行在ui线程中，在doInBackground()执行完毕后执行
     */
    @Override
    protected void onPostExecute(String params) {

        try {
            if (TextUtils.isEmpty(params)) {
                callback.onFailure("连接服务失败");
                return;
            }
            callback.onSuccess(params);
        } catch (Exception e) {
            e.printStackTrace();
            callback.onFailure(e.getMessage());
        }

    }


    /**
     * 在publishProgress()被调用以后执行，publishProgress()用于更新进度
     */
    @Override
    protected void onProgressUpdate(Integer... values) {

    }
}
